<!--
 * @Description: 实时监测
 * @Author: Ronda
 * @Date: 2022-02-16 09:30:08
 * @LastEditors: Ronda
 * @LastEditTime: 2022-05-23 15:20:33
-->
<template>
  <div>
    <el-row>
      <el-col :span="4">
        <div class="tree">
          <el-input placeholder="输入关键字进行过滤" v-model="filterText"></el-input>
          <el-tree @node-click="handler" class="filter-tree" :data="empData" :props="defaultProps"
            :filter-node-method="filterNode" ref="tree"></el-tree>
        </div>
      </el-col>
      <el-col :span="20">
        <div v-if="params.device_id">
          <el-alert title="实时监测数据（每10秒自动更新）" type="info" :closable="false" show-icon />
          <el-table :data="monitors" style="width: 100%" v-loading="loading">
            <el-table-column prop="update_time" label="更新时间" align="center" width="180">
              <template slot-scope="scope">
                {{ scope.row.update_time || '实时更新中...' }}
              </template>
            </el-table-column>
            <el-table-column prop="tsp" label="TSP" align="center"></el-table-column>
            <el-table-column prop="pm10" label="PM10" align="center"></el-table-column>
            <el-table-column prop="pm25" label="PM2.5" align="center"></el-table-column>
            <el-table-column prop="temperature" label="温度(℃)" align="center"></el-table-column>
            <el-table-column prop="humidity" label="湿度(%RH)" align="center"></el-table-column>
            <el-table-column prop="wind_direction" label="风向" width="120"></el-table-column>
            <el-table-column prop="wind_speed" label="风速(m/s)" align="center"></el-table-column>
          </el-table>

          <!-- 分页控件 -->
          <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
            :current-page="params.page" :page-sizes="[10, 20, 50]" :page-size="params.pagesize"
            layout="total, sizes, prev, pager, next" :total="totalCount" style="margin-top: 20px;"></el-pagination>
        </div>
        <div v-else style="text-align: center;color:red;line-height:50px">
          请选择设备进行查看
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { get } from '@/utils/request'

export default {
  data() {
    return {
      filterText: '',
      empData: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      params: {

        page: 1,
        pageSize: 100,
        engineer_id: 0,
        device_id: 0,
      },
      monitors: [],
      timer: null,
      loading: false,
      totalCount: 30
    }
  },
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val)
    }
  },
  methods: {
    handler(val) {
      if (!val.children) {
        this.clearTimer()
        this.params.engineer_id = val.engineer_id;
        this.params.device_id = val.id;
        this.startPolling()
      }
    },
    async getMonitor() {
      try {
        this.loading = true
        const res = await get('/monitor/pageQueryTodayData', this.params)
        // 假设接口返回格式：{ data: { list: [], total: 100 } }
        this.monitors = res.data.list || [res.data]
        this.totalCount = res.data.total || 1
      } finally {
        this.loading = false
      }
    },
    // 分页大小改变
    handleSizeChange(val) {
      this.params.pageSize = val
      this.params.page = 1 // 重置到第一页
      this.getMonitor()
    },
    // 当前页码改变
    handleCurrentChange(val) {
      this.params.page = val
      this.getMonitor()
    },
    startPolling() {
      this.getMonitor()
      this.timer = setInterval(() => {
        this.getMonitor()
      }, 10000)
    },
    clearTimer() {
      if (this.timer) {
        clearInterval(this.timer)
        this.timer = null
      }
    },
    filterNode(value, data) {
      if (!value) return true
      return data.name.indexOf(value) !== -1
    },
    async getEmpData() {
      const res = await get('/dashboard/findEngineerDeviceTree')
      this.empData = res.data
    }
  },
  created() {
    this.getEmpData()
  },
  beforeDestroy() {
    this.clearTimer()
  }
}
</script>

<style lang="scss" scoped>
.tree {
  height: calc(100vh - 60px);
  border-right: 1px solid #ededed;
  padding-right: 10px;
  margin-right: 10px;
}

.el-alert {
  margin-bottom: 15px;
}
</style>